import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'csimage.dart';
import 'keybean.dart';
import 'toast.dart';

class TwoPage extends StatefulWidget {
  @override
  State<TwoPage> createState() {
    return TwoPageState();
  }
}

class TwoPageState extends State<TwoPage> with AutomaticKeepAliveClientMixin {
  int people = 0;
  int piao = 0;
  int count = 0;
  String message = "";
  List list = new List();
  ScrollController _controller;

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    _controller = ScrollController();
    initData();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  void initData() async {
    var url = 'https://mock.yonyoucloud.com/mock/9623/shoplist';
//        'https://api.yunxuekeji.cn/yunxue_app_api/content/getColumn/1/73334';
    String msg = "暂无数据";
    try {
      var data;
      var response = await Dio().get(url);
      if (response.statusCode == 200) {
        data = response.data;
      } else {
        msg = "当前网络质量不佳";
      }

      if (data['code'] == 1) {
        if (mounted) {
          setState(() {
            list.addAll(new List.from(data['goods_list']));
          });
        }
      } else {
        msg = data['msg'];
      }
    } on Exception catch (e) {
      msg = "服务器异常" + e.toString();
    }

    if (mounted) {
      setState(() {
        message = msg;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage("images/defult_back.jpg"), fit: BoxFit.fill),
        ),
        child: Container(
          width: KeyBean.width,
          child: ListView(
            shrinkWrap: true,
            children: [
              MyImage(
                url: "",
                w: double.infinity,
              ),
              Row(
                mainAxisSize: MainAxisSize.max,
                children: [
                  Expanded(
                    flex: 1,
                    child: Container(
                      color: Colors.indigo,
                      width: double.infinity,
                      padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                      alignment: Alignment.center,
                      child: Column(
                        children: [
                          Text(
                            "参与者",
                            style: TextStyle(color: Colors.white, fontSize: 18),
                          ),
                          Text(
                            people.toString(),
                            style: TextStyle(color: Colors.white, fontSize: 18),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Container(
                      color: Colors.indigo,
                      width: double.infinity,
                      margin: EdgeInsets.fromLTRB(1, 0, 1, 0),
                      padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                      child: Column(
                        children: [
                          Text(
                            "投票数",
                            style: TextStyle(color: Colors.white, fontSize: 18),
                          ),
                          Text(
                            piao.toString(),
                            style: TextStyle(color: Colors.white, fontSize: 18),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    flex: 1,
                    child: Container(
                      color: Colors.indigo,
                      width: double.infinity,
                      padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                      child: Column(
                        children: [
                          Text(
                            "访问量",
                            style: TextStyle(color: Colors.white, fontSize: 18),
                          ),
                          Text(
                            count.toString(),
                            style: TextStyle(color: Colors.white, fontSize: 18),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
              InkWell(
                onTap: () {},
                child: Container(
                  width: double.infinity,
                  alignment: Alignment.center,
                  margin: EdgeInsets.fromLTRB(20, 10, 20, 10),
                  padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                  child: Text(
                    "我要报名",
                    style: TextStyle(color: Colors.white, fontSize: 18),
                  ),
                  decoration: BoxDecoration(
                    color: Colors.indigo,
                    border: new Border.all(width: 1, color: Colors.indigo),
                    borderRadius: BorderRadius.all(Radius.circular(15.0)),
                  ),
                ),
              ),
              list.length > 0
                  ? GridView.builder(
                      padding: const EdgeInsets.all(10),
                      shrinkWrap: true,
                      physics: new NeverScrollableScrollPhysics(),
                      controller: _controller,
                      itemCount: list.length,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 1,
                        mainAxisSpacing: 10.0,
                        crossAxisSpacing: 10.0,
                        childAspectRatio: 5,
                      ),
                      itemBuilder: (BuildContext context, int position) {
                        var at = list.elementAt(position);
                        return Row(
//                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Stack(
                              alignment: Alignment.center,
                              children: [
                                position < 3
                                    ? MyImage(
                                        url: at['image_url'],
                                        w: 30,
                                        h: 30,
                                      )
                                    : Container(),
                                Container(
                                  alignment: Alignment.center,
                                  width: 30,
                                  height: 30,
                                  child: Text(
                                    position.toString(),
                                    style: TextStyle(
                                        color: position < 2
                                            ? Colors.red
                                            : Colors.black87),
                                  ),
                                ),
                              ],
                            ),
                            Expanded(
                              flex: 1,
                              child: Container(
                                margin: EdgeInsets.fromLTRB(5, 0, 5, 0),
                                child: Text(
                                  at['short_name'],
                                  maxLines: 1,
                                  style: TextStyle(
                                      color: position < 2
                                          ? Colors.red
                                          : Colors.black87),
                                ),
                              ),
                            ),
                            Expanded(
                              flex: 3,
                              child: Container(
                                width: double.infinity,
                                child: Text(
                                  at['short_name'],
                                  maxLines: 1,
                                  style: TextStyle(
                                      color: position < 2
                                          ? Colors.red
                                          : Colors.black87),
                                ),
                              ),
                            ),
                          ],
                        );
                      })
                  : InkWell(
                      onLongPress: () {
                        Clipboard.setData(ClipboardData(text: message));
                        Toast.toast(context, msg: "已经复制到粘贴板");
                      },
                      child: Text(message),
                    ),
            ],
          ),
        ),
      ),
    );
  }
}
